package com.zhyz.bookstore.dao.impl;

import com.zhyz.bookstore.dao.OrderItemsDao;
import com.zhyz.bookstore.entity.OrderItems;
import com.zhyz.bookstore.util.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderItemsDaoImpl implements OrderItemsDao {





    @Override
    public List<OrderItems> queryOrderItemsByOrderId(String orderId) {
        Connection conn = null;
        PreparedStatement pstat = null;
        ResultSet rs = null;
        List<OrderItems> orderItemsList = null;

        orderItemsList = new ArrayList<>();
        String sql = "SELECT * FROM orderitems WHERE ordernum =?";
        conn = JDBCUtils.getconnection();
        try {
            pstat = conn.prepareStatement(sql);
            pstat.setString(1, orderId);
            rs = pstat.executeQuery();
            while (rs.next()) {
                OrderItems oi = new OrderItems(rs.getString(1),rs.getInt(2),rs.getDouble(3),rs.getString(4),rs.getString(5));
                orderItemsList.add(oi);
            }
        } catch(SQLException e){
            throw new RuntimeException(e);
        } finally{
            JDBCUtils.close(rs, pstat, conn);
        }
        return orderItemsList;
    }
}
